
#include "MainApplication.h"
#include "MainFrame.h"
#include "MainDocument.h"
#include "MainView.h"
#include "FontDefinition.h"

#include <wx/splash.h>
#include <wx/stdpaths.h>
#include <wx/filename.h>

IMPLEMENT_APP( MainApplication );

bool MainApplication::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    // Create the document manager
    mDocManager = new wxDocManager();
    mDocManager->SetMaxDocsOpen( 1 );       // SDI

    wxDocTemplate* docTemplate = new wxDocTemplate( mDocManager, _( "Hieroglyphics Document" ), wxT( "*.hieroglyph" ),
                wxEmptyString, wxT( "hieroglyph" ), wxT( "Hieroglyphics Doc" ),
                wxT( "Hieroglyphics View" ), CLASSINFO( MainDocument ), CLASSINFO( MainView ) );

    MainFrame* frame = new MainFrame( mDocManager, NULL );

    // Load the splash image
    wxBitmap bitmap;
    wxString appDir = wxStandardPaths::Get().GetExecutablePath();
    wxFileName appPath( appDir );
    appDir = appPath.GetPath() + wxFileName::GetPathSeparator();
    bool success = bitmap.LoadFile( appDir + wxT( "images/splash_image.png" ), wxBITMAP_TYPE_ANY );

    // Display the splash window
    wxSplashScreen* splash = 0;
    if ( success )
    {
        splash = new wxSplashScreen( bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT, 1500, frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER | wxSTAY_ON_TOP );
    }

    // Initialize the font map:
    FontMap::GetInstance();

    // Show the frame
    SetTopWindow( frame );
    frame->Centre();
    frame->SetIcon( wxIcon( appDir + wxT( "images/icon.ico" ), wxBITMAP_TYPE_ICO ) );
    frame->Show();

    // Delete the splash screen
    //delete splash;

    return true;
}

int MainApplication::OnExit()
{
    wxDELETE( mDocManager );
    return wxApp::OnExit();
}

wxDocManager* MainApplication::GetDocManager()
{
    return mDocManager;
}
